package com.drojian.demo3.dataStorage.SQLite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.drojian.demo.R;

public class SQLiiteActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part3_activity_sqliite);

        Button createDatabase = findViewById(R.id.sql_btn);
        Button add = findViewById(R.id.sql_add);
        Button update = findViewById(R.id.sql_update);
        Button delete = findViewById(R.id.sql_delete);
        Button select = findViewById(R.id.sql_select);
        EditText query_all = findViewById(R.id.query_all);
        dbHelper = new MyDatabaseHelper(this, "database01.db", null, 1);
        createDatabase.setOnClickListener(v-> dbHelper.getWritableDatabase());
        add.setOnClickListener(
                v->{
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("name","张三");
                    values.put("age",18);
                    db.insert("user",null,values);
                    values.clear();
                    values.put("name","李四");
                    values.put("age",18);
                    db.insert("user",null,values);
                    values.clear();
                    values.put("name","张三");
                    values.put("age",19);
                    db.insert("user",null,values);
                    values.clear();
                    values.put("name","曹六");
                    values.put("age",18);
                    db.insert("user",null,values);

        });
        update.setOnClickListener(
                v->{
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("name","王五");
                    db.update("user",values,"age = ?",new String[]{"19"});
        });
        delete.setOnClickListener(
                v->{
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    db.delete("user","age = ?",new String[]{"18"});
                }
        );
        select.setOnClickListener(
                v->{
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    Cursor user = db.query("user", null, null, null, null, null, null);
                    String str = "";
                    if(user.moveToFirst()){

                        do{
                            int column_name = user.getColumnIndex("name");
                            String name = user.getString(column_name);
                            int column_age = user.getColumnIndex("age");
                            int age = user.getInt(column_age);
                            str=str+"姓名："+name+",  年龄："+age+"\n";
                        }while (user.moveToNext());
                    }
                    query_all.setText(str);
                }
        );
    }
}